
package com.william.killian.trivia.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.william.killian.trivia.R;
import com.william.killian.trivia.model.Quiz;

public class Util
{
    private static File path;
    public static ArrayList<Quiz> quizzes;
    public static int current = 0;
    
    public static void CopyFromAssetsToSDCard (Context context)
    {
        File f = new File (context.getFilesDir (), "dummy");
        if (f.mkdirs ())
        {
            try
            {
                String[] files = context.getAssets ().list ("");
                for (String assetFileName : files)
                {
                    InputStream in = context.getAssets ().open (assetFileName);
                    OutputStream out = context.openFileOutput (assetFileName, Context.MODE_PRIVATE);
                    byte[] buf = new byte [1024];
                    int len;
                    while ((len = in.read (buf)) > 0)
                    {
                        out.write (buf, 0, len);
                    }
                    in.close ();
                    out.close ();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace ();
            }
        }
    }
    
    public static File getApplicationDirectory ()
    {
        path.mkdirs ();
        return path.getAbsoluteFile ();
    }
    
    public static File getHighScoresDirectory ()
    {
        File f = new File (Util.getApplicationDirectory (), "highscores");
        f.mkdirs ();
        return f;
    }

    public static void ShowErrorDialog (Activity act)
    {
        new AlertDialog.Builder(act)
            .setCancelable (false)
            .setMessage (R.string.text_error)
            .setIcon (R.drawable.ic_cross)
            .setTitle (R.string.error)
            .setPositiveButton (R.string.kill, new OnClickListener()
            {
                public void onClick (DialogInterface arg0, int arg1)
                {
                    System.exit (-1);
                }
            }).show ();
    }

    public static void SetAppPath (File filesDir)
    {
        path = filesDir;
    }

    public static Quiz currentQuiz ()
    {
        return quizzes.get (Util.current);
    }
}
